package com.erpoa.service.impl;

import com.erpoa.annotation.DataBase;
import com.erpoa.constants.DataSourceNames;
import com.erpoa.entity.RecMatEntity;
import com.erpoa.mapper.BatchUtilMapper;
import com.erpoa.service.BatchUtilService;
import com.erpoa.utils.PDAException;
import com.erpoa.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author chenjimin
 * @Title: BatchUtilServiceImpl
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/9/228:59
 */
@Service
public class BatchUtilServiceImpl implements BatchUtilService {
    @Autowired
    private BatchUtilMapper batchUtilMapper;


    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public List<Float> getErpMargin(RecMatEntity recMatEntity, String type) throws PDAException {
        List<Float> list = new ArrayList<Float>();
        BigDecimal sumyQty = new BigDecimal(0); // MES工单应发数
        float difQty = 0; // 差异量
        float qty = 0; // 本次发料数量
        //作业类型
        HashMap cptemp = this.batchUtilMapper.selectCpByParam(recMatEntity.getfOrderNo(), recMatEntity.getfMatCode());
        if (StringUtil.isEmpty(cptemp)) {
            throw new PDAException("工单:" + recMatEntity.getfOrderNo() + " 料号:" + recMatEntity.getfMatCode() + "获取作业类型失败!");
        }
        //作业编码
        String fProcss = cptemp.get("FPROCSS") == null ? "" : cptemp.get("FPROCSS").toString();
        //单位类型
        String fUnit = cptemp.get("FUNIT") == null ? "" : cptemp.get("FUNIT").toString();
        // QPA
        HashMap qpaTemp = this.batchUtilMapper.selectQpaByPatam(recMatEntity.getfOrderNo(), recMatEntity.getfMatCode(), fProcss, fUnit);
        if (StringUtil.isEmpty(qpaTemp)) {
            throw new PDAException("工单:" + recMatEntity.getfOrderNo() + " 料号:" + recMatEntity.getfMatCode() + "获取QPA失败!");
        }
        // erp应发量
        BigDecimal erpyQty = new BigDecimal(qpaTemp.get("SFA05") == null ? "0" : qpaTemp.get("SFA05").toString());
        // 成品笔头发料向上取整
        BigDecimal QPA = new BigDecimal(qpaTemp.get("SFA161") == null ? "0" : qpaTemp.get("SFA161").toString());
        // 工单应发数
        if ("笔头".equals(recMatEntity.getfType())) {
            sumyQty = this.batchUtilMapper.selectCpCeilByParam(recMatEntity.getfOrderNo(), QPA);
        } else if ("油墨".equals(recMatEntity.getfType()) || "硅油".equals(recMatEntity.getfType())) {
            sumyQty = this.batchUtilMapper.selectCpRoundByParam(recMatEntity.getfOrderNo(), QPA);
        }
        // mes工单总已发量
        BigDecimal sumFrspqty = this.batchUtilMapper.selectOverOutMatByParam(recMatEntity.getfOrderNo(), recMatEntity.getfMatCode());
        BigDecimal fWeight = new BigDecimal(String.valueOf(recMatEntity.getfWeight()));
        difQty = sumyQty.subtract(erpyQty).floatValue();
        if (type.equals("批次发料") && difQty != 0) {
            int zQty = (int) difQty;
            if (sumFrspqty.intValue() + fWeight.intValue() == erpyQty.intValue() && difQty <= 30) {
                //只改MES领发料
                qty = fWeight.intValue() + zQty;
            } else if (sumFrspqty.intValue() + fWeight.intValue() == sumyQty.intValue() && Math.abs(difQty) <= 30) {
                //只改ERP改中间表
                qty = fWeight.intValue() - zQty;
            }
        } else if (type.equals("批次退料") && difQty != 0) {
            if (sumFrspqty.subtract(fWeight.add(sumyQty.subtract(erpyQty))).floatValue() == erpyQty.floatValue() && Math.abs(difQty) <= 0.1) {
                qty = fWeight.floatValue() + difQty;
            }
        } else if (type.equals("循环料流转") && difQty != 0) {
            if (sumFrspqty.subtract(fWeight.add(sumyQty.subtract(erpyQty))).floatValue() == erpyQty.floatValue() && Math.abs(difQty) <= 0.1) {
                qty = fWeight.floatValue() + difQty;
            }
        }
        list.add(difQty);
        list.add(qty);
        return list;
    }

}
